<?php

class Core_Model_Promotion_Concessions extends Dcmp_Model_Tag {

    public function __construct() {
        parent::__construct();
        $this->tagType = 'concessions';
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_promotion_concessions');
    }

//    protected function insertSelf($transaction) {
//        $data = $this->getAllData();
//        if ($this->isArrayData('poi_ids')) {
//            $data['poi_ids'] = implode(',', $data['poi_ids']);
//        }
//        $this->getTable()->insert($data, $transaction);
//        $this->setId($this->getTable()->getInsertId());
//    }

    public function afterInsert($transaction) {
        if ($this->isArrayData('poi_ids')) {
            $this->getTable()->insertPoiRelation($this->getId(), $this->getPoiIds(), $transaction);
        }
        parent::afterInsert($transaction);
    }

    protected function beforeUpdate($transaction) {
//        $this->setPoiIds(Kd_Util_Array::removeEmpty($this->getPoiIds()));
        parent::beforeUpdate($transaction);
    }

//    protected function updateSelf($transaction){
//        $data = $this->getChangedData();
//        if ($this->propertyIsChanged('poi_ids')) {
//            $data['poi_ids'] = implode(',', $data['poi_ids']);
//        }
//        $data['id'] = $this->getId();
//        $this->getTable()->update($data, $transaction);
//    }

    public function afterUpdate($transaction) {
        if ($this->propertyIsChanged('poi_ids')) {
            $this->getTable()->deletePoiRelation($this->getId());
            if ($this->isArrayData('poi_ids')) {
                $this->getTable()->insertPoiRelation($this->getId(), $this->getPoiIds(), $transaction);
            }
        }
        parent::afterUpdate($transaction);
    }

    public function getCategory() {
        if (!$this->issetData('category') && $this->issetData('category_id')) {
            $this->data['category'] = App::getModel('promotion/concessions_category')->load($this->getCategoryId());
        }
        return $this->data['category'];
    }

    public function getCustomers() {
        return App::getModel('customer/collection_detail')
                ->innerJoin('{t}promotion_concessions_poi', 'p', 'a.id', 'p.poi_id')
                ->eq('p.concessions_id', ':concessionsId')
                ->addParameter('concessionsId', $this->getId());
    }

    public function getFirstCustomer() {
        $result = $this->getCustomers()->load();
        if (count($result) > 0) {
            return $result[0];
        }
        return $result;
    }

    public function getFirstPicture() {
        if ($this->issetData('picture')) {
            $result = explode(',', $this->getPicture());
            return $result[0];
        }
        return '';
    }

    public function updateClickCount($id) {
        Kd_Data_Factory::getUpdateDcl()
                ->into('{t}promotion_concessions')
                ->value('click_count', Kd_Data_Dcl_Fn::addition('click_count', 1))
                ->where()->eq('id', ':id:i')->closest()
                ->addParameter('id', $id)
                ->execute();
        return $this;
    }

    protected function initializeTable($table) {
        $table->setName('{t}promotion_concessions');
        $table->createColumn('id', 'integer', TRUE, TRUE);
        $table->createColumn('name', 'string');
        $table->createColumn('category_id', 'integer');
        $table->createColumn('theme_id', 'guid');
        $table->createColumn('start_time', 'datetime');
        $table->createColumn('end_time', 'datetime');
        $table->createColumn('detail', 'string');
        $table->createColumn('condition', 'string');
        $table->createColumn('mode', 'string');
        $table->createColumn('picture', 'string');
        $table->createColumn('poi_ids', 'array');
        $table->createColumn('click_count', 'integer')->setDefaultValue(0);
        $table->createColumn('is_new', 'integer');
        $table->createColumn('is_hot', 'integer');
        $table->createColumn('is_top', 'integer');
        $table->createColumn('owner_id', 'integer');
        parent::initializeTable($table);
    }

}